package com.qiang.test02;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Send {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        //创建客户端
        Socket socket = new Socket("127.0.0.1", 15000);
//        File f1 = new File("D:\\JavaWEB.jpg");
//        File f2 = new File("D:\\Java基础.jpg");
//        String a1 = f1.getAbsolutePath();
//        String a2 = f2.getAbsolutePath();
//        lo:
//        while (true) {

        System.out.println("请输入上传文件的路径名");
        String s = sc.nextLine();
        File f1 = new File(s);

        if (f1.exists()) {
            if (f1.isFile()) {

                //如果成功匹配文件，就读取
                //创建输入流读取文件
                String absolutePath = f1.getAbsolutePath();
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(absolutePath));

                //从客户端获取输出流
                OutputStream os = socket.getOutputStream();
                BufferedOutputStream bos = new BufferedOutputStream(os);

                byte[] bb = new byte[1024];

                int len;
                while ((len = bis.read(bb)) != -1) {
                    bos.write(bb, 0, len);
                }
                socket.shutdownOutput();
                //break lo;

                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
                bis.close();
                //bos.close();
                socket.close();
            }
        } else {
            System.out.println("请重新输入");
        }
        //}
        //从服务器读取返回信息
//        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        String line;
//        while ((line = br.readLine()) != null) {
//            System.out.println(line);
//        }



        //从网络中获取返回信息
//        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        String line;
//        while ((line = br.readLine()) != null) {
//            System.out.println(line);
//        }
//        br.close();
//        socket.close();
    }
}
